package org.lonetree.tc.webapp.actions;

import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.lonetree.tc.core.Book.Isbn;
import org.lonetree.tc.core.BookComment;
import org.lonetree.tc.core.dao.BookDAO;
import org.lonetree.tc.core.dao.DAOFactory;

public class ListBookComment extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
            try{
                //the url to list all comments could be like listcomment.do?bookid=123
                //String bookID=request.getParameter("bookID");
                //or like listcomment.do?isbn=1234567890
                String isbn=request.getParameter("isbn");
                DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.FactoryImplementation.DB_DAO_FACTORY);
                BookDAO bao=factory.getBookDAO();
                Set<BookComment> comments=bao.getBook(new Isbn(isbn)).getComments();
                request.setAttribute("bookComments", comments);
                return mapping.findForward("success");
            }
            catch (Exception e)
            {
                	return mapping.findForward("failure");
            }
	}

}
